package hhm.android.library.utils

import android.content.Context
import android.widget.ImageView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.load.resource.bitmap.CircleCrop
import com.bumptech.glide.request.RequestOptions

class GlideUtils {
    companion object {
        fun loadImage(
            context: Context,
            url: String,
            view: ImageView,
            error: Int,
            loading: Int
        ) {
            Glide.with(context).load(url).placeholder(loading).error(error).into(view)
        }

        /**
         * 无任何缓存 任何点击，焦点切换，onResume 都会导致图片重新加载
         */
        fun loadCircleImageNoCache(
            context: Context,
            url: String,
            view: ImageView,
            error: Int,
            loading: Int
        ) {
            Glide.with(context).load(url)
                .apply(
                    RequestOptions().skipMemoryCache(true).diskCacheStrategy(DiskCacheStrategy.NONE)
                )
                .apply(RequestOptions.bitmapTransform(CircleCrop()))
                .placeholder(loading).error(error).into(view)
        }

        /**
         * 有缓存，在可选可切换图片的控件上不适用，因为有可能导致图片不刷新
         */
        fun loadCircleImage(
            context: Context,
            url: String,
            view: ImageView,
            error: Int,
            loading: Int
        ) {
            Glide.with(context).load(url)
                .apply(RequestOptions.bitmapTransform(CircleCrop()))
                .placeholder(loading).error(error).into(view)
        }
    }
}